﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MarginTrading.Entity;
using MarginTrading.Services;
using Microsoft.Reporting.WebForms;
using MarginTrading.Common;

namespace MarginTrading.site
{
    public partial class StatusReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }

            if (!IsPostBack)
            {
                DataTable advisor = AdvisorServices.GetAdvisorDDLDataTable();
                DataRow dr = advisor.NewRow();
                dr["NAME"] = "All";
                dr["CODE"] = "All";
                advisor.Rows.InsertAt(dr, 0);
                DataView dv = new DataView(advisor);
                AdvisorDropDownList.DataSource = advisor;
                AdvisorDropDownList.DataTextField = "NAME";
                AdvisorDropDownList.DataValueField = "CODE";
                AdvisorDropDownList.DataBind();
            }

            if (Session["UserType"].ToString() == "3")
            {
                AdvisorDropDownList.Visible = false;
                AdvisorLbl.Visible = false;
                SearchBtn.Visible = false;
                LoadReport(Session["UserName"].ToString().Trim(), "");
            }
            if (Session["UserType"].ToString() == "4")
            {
                ReportViewer1.Height = 500;
                AdvisorDropDownList.Visible = false;
                AdvisorLbl.Visible = false;
                SearchBtn.Visible = false;
                LoadReport("", Session["UserName"].ToString().Trim());
            }

        }

        protected void SearchBtn_Click(object sender, EventArgs e)
        {
            String ww = AdvisorDropDownList.Text;
            SearchBtn.Enabled = false;
            if (!String.IsNullOrEmpty(AdvisorDropDownList.Text))
            {
                LoadReport(AdvisorDropDownList.Text, "");
            }
            SearchBtn.Enabled = true;
        }

        private void LoadReport(string advisorId, string clientID)
        {
            try
            {
                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                Advisor advisor = AdvisorServices.GetAdvisorByCode(advisorId);

                if (advisor == null)
                {
                    //advisor = AdvisorServices.GetAdvisorByCode(advisorId.ToUpper());
                    //if (advisor == null)
                    //{
                    advisor = new Advisor();
                    advisor.Code = "All";
                    //}
                    //else
                    //{
                    //    advisorId = advisorId.ToUpper();
                    //}
                }
                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";


                if (clientID != "")
                {
                    ds.Value = ReportServices.GetMarginTradingStatusByClient(clientID, AgentCodeConfig.Value);
                }
                else if (advisorId != "All")
                {
                    ds.Value = ReportServices.GetMarginTradingStatusByAdvisor(advisorId, AgentCodeConfig.Value);
                }
                else
                {
                    ds.Value = ReportServices.GetMarginTradingStatusByAdvisor(AgentCodeConfig.Value);
                }



                ReportViewer1.LocalReport.ReportPath = @"Reports\MarginTradingStatus.rdlc";
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(ds);



                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                ReportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("AdvisorID",advisor.Code),
                        new ReportParameter("AdvisorName",string.Concat(advisor.Initials, 
                                                            " ",advisor.Name, 
                                                            " ", advisor.Surname) ),
                        new ReportParameter("ReportTitle",Reports.MARGIN_TRADING_TITLE)});


                //ReportViewer1.ZoomMode = ZoomMode.Percent;
                //ReportViewer1.ZoomPercent = 100;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }
    }
}
